<?php
/* * *********************************

  Rever 2012



  Quiz game

 * ********************************** */

// Setting up our environment for fb

require_once("facebook.php");

$config = array();

$config['appId'] = '521468797878527';

$config['secret'] = 'eaf599cc1ef944fd7a02cf1b9f54303d';

$config['fileUpload'] = true;

$facebook = new Facebook($config);



// trying to identificate user

$user_id = $facebook->getUser();



// is logged?

$signed_request = $facebook->getSignedRequest();



// useful for save our data and showing user the score
// its empty if the user has not played before the game

$app_data = $signed_request["app_data"];

// time function

function microtime_float()
{

  list($usec, $sec) = explode(" ", microtime());

  return ((float) $usec + (float) $sec);
}
?><!DOCTYPE HTML>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <title>Ponle Estrella Rally</title>

    <link rel="stylesheet" href="public/css/reset.css" type="text/css" />

    <link rel="stylesheet" href="public/css/main.css" type="text/css" />

    <link rel="stylesheet" href="public/js/fancybox/jquery.fancybox.css?v=2.0.6" type="text/css" media="screen" />



    <script language="javascript" src="public/js/jquery-1.7.2.min.js"></script>

    <script type="text/javascript" src="public/js/fancybox/jquery.fancybox.pack.js?v=2.0.6"></script>





    <script language="javascript" src="public/js/main.js"></script>

    <script>

      function reloadNew(param){ top.location.href="http://www.facebook.com/CervezaEstrella/app_521468797878527?app_data="+param; }

    </script>





<?php
// load the necesaries files to play the memory-match game

if (isset($app_data) && $app_data == 1) {
?>

      <link rel="stylesheet" href="public/css/game.css" type="text/css"/>

      <script language="javascript" src="public/js/crono.js"></script>

      <script language="javascript" src="public/js/game.js"></script>

    <?php
  }



  // load the show score screen

  if (isset($app_data) && $app_data == 2) {
    ?>



    <link rel="stylesheet" href="public/css/score.css" type="text/css"/>

    <script language="javascript" src="public/js/jquery.tinyscrollbar.min.js"></script>

    <script language="javascript" src="public/js/score.js"></script>



    <?php } ?>



  </head>



  <body>

    <div id="content">

      <div id="menu">

        <a id="opc-1" onclick="top.location.href='http://www.facebook.com/CervezaEstrella/app_521468797878527'"  href="javascript:;"></a>

        <a id="opc-3" onclick="top.location.href='http://www.facebook.com/CervezaEstrella/app_521468797878527?app_data=2'"  href="javascript:;"></a>

      </div><!-- menu //-->





      <div style="display:none">

        <div id="terms"></div>

      </div>



<?php
// the user has accepted our app =D woojo

  if ($user_id) {



    $fbuser = $facebook->api('/me');



    // store the name of the user

    $_SESSION['estrella_rally_username'] = $fbuser['name'];

    $_SESSION['id_facebook'] = $user_id;



    // we check if are gonna save our score or just play the game

    if (isset($app_data) && $app_data != '') {



      // the user wants to play, app_data is equal to 1

      if ($app_data == 1) {

        /*         * **********************************

          play the game screen

         * ********************************** */











        /*         * **********************************

          end of game screen

         * ********************************** */

        // the user wants to consult the scores, app_data is equal to 2
      } else {

        /*         * **********************************

          show score screen

         * ********************************** */
?>

        <!-- scores section //-->

        <div id="wrap-score">



          <div id="score-top"></div>



          <div id="scrollbar1">

            <div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>

            <div class="viewport">

              <div class="overview">



                <div id="score-middle">



<?php
        $hostname_conexion = "localhost";

        $database_conexion = "verticet_estrella";

        $username_conexion = "verticet_estrell";

        $password_conexion = "v-estr-lla-2012";

        $conexion = mysql_pconnect($hostname_conexion, $username_conexion, $password_conexion) or trigger_error(mysql_error(), E_USER_ERROR);

        mysql_query('SET NAMES "UTF8"', $conexion);

        mysql_query("use verticet_estrella", $conexion);



        // get the position of players

        @mysql_query("SET @rank=0;");



        $query_s = "select x.id_facebook xid_facebook, x.nombre xnombre, x.tiempo xtiempo, rank from crono_rally x join  ( SELECT *, @rownum:=@rownum+1 as rank FROM (SELECT * FROM crono_rally p ORDER BY tiempo ASC LIMIT 80) user_rank, (SELECT @rownum:=0) r )  c on c.id = x.id  order by rank asc";

        $get_pos = @mysql_query($query_s);

        while ($r_g = mysql_fetch_array($get_pos)) {





          // show the date to the user

          $seconds = ($r_g['xtiempo'] * 1);

          $hours = sprintf("%02d", floor($seconds / 3600));

          $mins = sprintf("%02d", floor(($seconds - ($hours * 3600)) / 60));

          if ($seconds > 60) {

            $_seconds = sprintf("%02d", floor($seconds % 60 % 60 % 60));
          } else {

            $_seconds = sprintf("%02d", floor((($seconds * 3600) / 60) / 60));
          }

          $ms = explode(".", $seconds); ?>

                <!-- item-score //-->

                <div class="item-score">

                  <div class="num-score"><?php echo sprintf("%02d", $r_g['rank']); ?></div>

                  <div class="bg-frame">

                    <img src="http://graph.facebook.com/<?php echo $r_g['xid_facebook']; ?>/picture" width="40" height="40" class="img-score" />

                  </div>

                  <div class="bg-frame-name">

                <?php echo substr($r_g['xnombre'], 0, 30); ?>

                  </div>

                  <div class="bg-frame-black">

                    <div class="c-time">

                      <div class="c-hora"><?php echo $mins; ?></div><div class="c-minuto"><?php echo $_seconds; ?></div><div class="c-segundo"><?php echo substr($ms[1], 0, 3); ?></div>

                      <div class="line-score"></div>

                    </div>

                  </div>

                  <div class="clear-score"></div>

                </div>

                <!-- item-score //-->

<?php } ?>



              </div><!-- score-middle //-->





            </div>

          </div>

        </div><!-- tiny //-->





        <div id="score-bottom"></div>

      </div><!-- wrap-score //-->

      <!-- scores section //-->



<?php
              /*               * **********************************

                end show score screen

               * ********************************** */
            }
          } else {

            // before play we check if the user already read the rules
?>



      <!-- first section //-->



      <!-- first section //-->



      <?php
          }



          // the user still hasnt accepted our app, we need it to accept!
        } else {



          // our combo config

          $params = array(
           'scope' => 'publish_stream, user_photos, read_stream, photo_upload, read_friendlists, friends_likes',
           'redirect_uri' => 'https://www.facebook.com/CervezaEstrella/app_521468797878527'
          );

          // please login user!

          $loginUrl = $facebook->getLoginUrl($params);

          echo '<script>top.location.href = "' . $loginUrl . '";</script>';
        }
      ?>

      <a href="#terms" id="footer-link"></a>

    </div><!-- content //-->



  </body>

</html>

